package com.fintecher.controller;

import com.alibaba.fastjson.JSON;
import com.fintecher.pojo.Power;
import com.fintecher.pojo.Role;
import com.fintecher.pojo.User;
import com.fintecher.service.IPowerService;
import com.fintecher.service.IRoleService;
import com.fintecher.utils.Page;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2017/2/8.
 */
@Controller
@RequestMapping("/power")
public class PowerController {
    @Autowired
    private IPowerService powerService;
    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/toUGM")
    private String toUGM(){

        return "redirect:/role/disps";
    }



    @RequestMapping(value = "disp1",method = RequestMethod.POST)
    private String disp1(){
        return "operate_account";
    }








    @RequestMapping(value = "disp",method = RequestMethod.GET)
    private String disp(){
        return "user_manage";
    }


    /**
     * 权限页面查询
     * @param request
     * @return
     */
    @RequestMapping(value = "selPower",method = RequestMethod.GET)
    private ModelAndView selPower(HttpServletRequest request, Role role, HttpSession session, Model model,String pageNow){

        /**
         * 从session中获得登录者的id，查看本帐号下所有的用户组
         */
        User user = (User)session.getAttribute( "user" );
        role.setUserId( user.getSuperiorId());



        Page page = null;//new 对象在下面
        int totalCount = roleService.getCount(role.getUserId());//获取当前用户总记录条数

        if(pageNow != null && pageNow!= "") {//防止出现空指针异常

            int pageNow1 = Integer.parseInt(pageNow);
            page = new Page(totalCount, pageNow1);//这样写的好处，判断完成外面可以继续调用
        }else {
            pageNow="1";
            int pageNow1 = Integer.parseInt(pageNow);
            page = new Page(totalCount, pageNow1);
        }
        role.setPage(page);
        model.addAttribute("page",page);
        List<Role> roleList =  roleService.selRoleGroupUser(role);
        logger.info(JSON.toJSONString(roleList));
        request.setAttribute("rList",roleList);


/*
* 根据登录用户id查询用户所有权限
* */
        List<Role> roleLists = roleService.selAllPower(role);
        request.setAttribute("rol",roleLists);
        logger.info(JSON.toJSONString(roleList));

        /**
         * 查看所有的权限以及子权限
         */
        List<Power> poweList = powerService.selPower();

        logger.info(JSON.toJSONString(poweList));

        request.setAttribute("pList",poweList);

        return new ModelAndView("user_group_manage");
    }

   /* @RequestMapping("/main")
    public String  main(@RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage,Model model, HttpServletResponse response,HttpServletRequest request)throws IOException {
        List<Role> lists = roleService.selectUserList();
        model.addAttribute("userLists", lists);//回显用户信息
        model.addAttribute("pagemsg", roleService.findByPage(currentPage));//回显分页数据
        return "logins";
    }*/

    /*
    *
    * 账户信息管理
    *子模块查询
    * */
    @RequestMapping("/toAM")
    public String toAM(Role role,HttpSession session,HttpServletRequest request,Model model){
        User user=(User)session.getAttribute("user");
        user.getId();
        role.setUserId(user.getId());
        List<Role> roleList = roleService.selAllPower(role);
        logger.info(JSON.toJSONString(roleList));
        request.setAttribute("roles",roleList);
        model.addAttribute("user",user.getCompanyName());
        return "account_manage";
    }

    /*
    * 操作用户
    * 子模块查询
    *
    * */
    @RequestMapping("/toPower")
    public String toPower(Role role,HttpSession session,HttpServletRequest request){
        User user=(User)session.getAttribute("user");
        role.setUserId(user.getId());
        List<Role> roleList = roleService.selAllPower(role);
        logger.info(JSON.toJSONString(roleList));
        request.setAttribute("roles",roleList);
        return "operate_account";
    }


}
